﻿using DBQLTV;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class TheLoaiSachDAO
    {
        public List<string> GetListTheLoaiSach()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<string> result = new List<string>();
                List<THELOAISACH> lTheLoai = entity.THELOAISACHes.ToList();
                foreach (THELOAISACH theloai in lTheLoai)
                {
                    result.Add(theloai.TenTheLoaiSach);
                }
                return result;
            }
        }
        public bool InsertTheLoaiSach(string TenTheLoaiSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                THELOAISACH theLoai = entity.THELOAISACHes.FirstOrDefault(o => o.TenTheLoaiSach == TenTheLoaiSach);
                if (theLoai != null)
                    return false;

                THELOAISACH theloaisach = new THELOAISACH()
                {
                    TenTheLoaiSach = TenTheLoaiSach
                };

                entity.THELOAISACHes.AddObject(theloaisach);
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteTheLoaiSach(string TenTheLoaiSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                THELOAISACH theloaiSach = entity.THELOAISACHes.FirstOrDefault(o => o.TenTheLoaiSach.Equals(TenTheLoaiSach));
                if (theloaiSach != null)
                {
                    entity.THELOAISACHes.DeleteObject(theloaiSach);
                    entity.SaveChanges();
                    return true;
                }
                return false;
            }
        }
        public static THELOAISACH GetTheLoaiSachByName(string TenTheLoaiSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                THELOAISACH theloaisach = entity.THELOAISACHes.FirstOrDefault(o => o.TenTheLoaiSach == TenTheLoaiSach);
                return theloaisach;
            }
        }
        public THELOAISACH GetTheLoaiByKey(int MaTheLoai)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                THELOAISACH theloaisach = entity.THELOAISACHes.FirstOrDefault(o => o.MaTheLoaiSach == MaTheLoai);
                return theloaisach;
            }
        }
    }
}
